package Project;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import Gameplay.Level;
/**
 * This class is to draw all the graphics for 
 * the game's local maps, and controls for next
 * level or previous level.
 * 
 * @author	Brian Wetherbee
 * 			Ryan McNamara
 * 			Zeng Yang
 *
 *
 */
public class LocalMap extends Menu {
	/*@ public invariant menuPosition <= levelsBeaten; @*/
	/*@ public invariant Main.UI = this; @*/
	
	public /*@ spec_public @*/ int levelsBeaten;
	public /*@ spec_public @*/ int worldNumber;
	
	public LocalMap() {
		super(Constants.NUM_LEVELS_PER_WORLD);
		//shouldn't make a local map this way, since we need to know which number local map it is
	}
	
	public LocalMap(int mapNo) {
		super(Constants.NUM_LEVELS_PER_WORLD);
		worldNumber = mapNo;
		updateInformation();
		
		initComponents();
	}
	
	public void initComponents(){
		addKeyListener(this);
        addMouseListener(this);
        repaint();
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		//MyGraphics g = new MyGraphics(gg);
		g.setColor(Color.BLACK);
		g.fillRect(0,0,1672,968);
    	
    	//Draw UI
    	g.setColor(new Color(64,64,64));
    	g.fillRect(0, 0, 1672, 64);
    	g.fillRect(0, 904, 1672, 64);
    	g.setColor(new Color(128,128,128));
    	g.drawRect(0, 0, 1672, 64);
    	g.drawRect(0, 904, 836, 64);
    	g.drawRect(836, 904, 836, 64);
    	
    	g.setColor(Color.WHITE);
    	g.setFont(new java.awt.Font("DialogInput",1,48));
    	FontMetrics f = g.getFontMetrics(new java.awt.Font("DialogInput",1,48));
    	int stringX = 0;
    	int stringY = 0;
    	
		if(worldNumber == 0) {
			stringX = f.stringWidth("Mars");
	    	stringY = f.getHeight();
	    	g.drawString("Mars", 836 - (stringX/2), 48 - ((64 - stringY)/2));
	    	
	    	g.setColor(new Color(127,0,0));
	        g.fillOval(480,128,712,712);
	        g.setColor(new Color(64,0,0));
	        g.drawOval(480,128,712,712);
		} else if(worldNumber == 1) {
			stringX = f.stringWidth("Jupiter");
	    	stringY = f.getHeight();
	    	g.drawString("Jupiter", 836 - (stringX/2), 48 - ((64 - stringY)/2));
	    	
	    	g.setColor(new Color(255,106,0));
	    	g.fillOval(480,128,712,712);
	    	g.setColor(new Color(128,53,0));
	    	g.drawOval(480,128,712,712);
		} else if(worldNumber == 2) {
			stringX = f.stringWidth("Saturn");
	    	stringY = f.getHeight();
	    	g.drawString("Saturn", 836 - (stringX/2), 48 - ((64 - stringY)/2));
	    	
	    	g.setColor(new Color(127,51,0));
	    	g.fillOval(480,128,712,712);
	    	g.setColor(new Color(64,26,0));
	    	g.drawOval(480,128,712,712);
	    	
		} else if(worldNumber == 3) {
			stringX = f.stringWidth("Uranus");
	    	stringY = f.getHeight();
	    	g.drawString("Uranus", 836 - (stringX/2), 48 - ((64 - stringY)/2));
	    	
	    	g.setColor(new Color(0,255,255));
	    	g.fillOval(480,128,712,712);
	    	g.setColor(new Color(0,128,128));
	    	g.drawOval(480,128,712,712);
		} else if(worldNumber == 4) {
			stringX = f.stringWidth("Neptune");
	    	stringY = f.getHeight();
	    	g.drawString("Neptune", 836 - (stringX/2), 48 - ((64 - stringY)/2));

	    	g.setColor(new Color(0,148,255));
	    	g.fillOval(480,128,712,712);
	    	g.setColor(new Color(0,74,128));
	    	g.drawOval(480,128,712,712);
		} else if(worldNumber == 5) {
			stringX = f.stringWidth("Pluto");
	    	stringY = f.getHeight();
	    	g.drawString("Pluto", 836 - (stringX/2), 48 - ((64 - stringY)/2));
	    	
	    	g.setColor(new Color(128,128,128));
	    	g.fillOval(480,128,712,712);
	    	g.setColor(new Color(64,64,64));
	    	g.drawOval(480,128,712,712);
		} else if(worldNumber == 6) {
			stringX = f.stringWidth("Mayacta");
	    	stringY = f.getHeight();
	    	g.drawString("Mayacta", 836 - (stringX/2), 48 - ((64 - stringY)/2));
	    	
	    	g.setColor(new Color(255,0,0));
	    	g.fillOval(480,128,712,712);
	    	g.setColor(new Color(128,0,0));
	    	g.drawOval(480,128,712,712);
		}
		
		g.setColor(Color.WHITE);
		g.drawLine(644,484,1028,484);
		
		g.setColor(new Color(160,160,160));
		g.fillOval(612, 452, 64, 64);
		g.fillOval(804, 452, 64, 64);
		g.fillOval(996, 452, 64, 64);
		
		drawSelector(g);
	}

	/*@ ensures menuPosition = (menuPosition + 1) % levelsBeaten; @*/
	@Override
    public void increaseHighlight()
    {
        setOption((getOption() + 1) % (levelsBeaten + 1));
        repaint();
    }
    
    @Override
    public void decreaseHighlight()
    {
        setOption((getOption() - 1) % (levelsBeaten + 1));
        if(getOption() == -1) {
        	setOption(levelsBeaten);
        }
        
        repaint();
    }

    
    @Override
    public void updateInformation(){
    	levelsBeaten = SaveFile.getCurrentFile().getLevelsBeaten();
		if(levelsBeaten < (worldNumber + 1) * Constants.NUM_LEVELS_PER_WORLD + worldNumber)
		{
			levelsBeaten -= worldNumber*Constants.NUM_LEVELS_PER_WORLD + worldNumber;
		} else {
			System.out.println("you have beat all the levels on this world");
			levelsBeaten = Constants.NUM_LEVELS_PER_WORLD - 1;
		}
    }
    
	@Override
	public void select() {
		UI toAdd = new Level(getOption()+(worldNumber*Constants.NUM_LEVELS_PER_WORLD + worldNumber),this,false);
		Main.setCurrentUI(toAdd);
		((Level)toAdd).startLevel();
	}
	
	public void exitMenu() {
		UI toAdd = new GalaxyMap(worldNumber * 2);
		Main.setCurrentUI(toAdd);
	}
	
	public void drawSelector(Graphics g) {
		g.setColor(new Color(64,64,64));
    	g.fillRect(0, 904, 836, 64);
    	g.setColor(new Color(128,128,128));
    	g.drawRect(0, 904, 836, 64);
    	
    	FontMetrics f = g.getFontMetrics(new Font("DialogInput",1,48));
    	int nameX = 0;
    	int nameY = 0;
		
		g.setColor(Color.WHITE);
		if(getOption() == 0) {
			g.drawOval(612, 452, 64, 64);
			nameX = f.stringWidth("Level " + (worldNumber + 1) + "-1");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("Level " + (worldNumber + 1) + "-1", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
		} else if(getOption() == 1) {
			g.drawOval(804, 452, 64, 64);
			nameX = f.stringWidth("Level " + (worldNumber + 1) + "-2");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("Level " + (worldNumber + 1) + "-2", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
		} else if(getOption() == 2) {
			g.drawOval(996, 452, 64, 64);
			nameX = f.stringWidth("Level " + (worldNumber + 1) + "-3");
        	nameY = f.getHeight();
        	g.setColor(Color.WHITE);
        	g.drawString("Level " + (worldNumber + 1) + "-3", 418 - (nameX / 2), 952 - ((64 - nameY) / 2));
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == Constants.controlLeft)
        {
            decreaseHighlight();
        }
        
        if(e.getKeyCode() == Constants.controlRight)
        {
            increaseHighlight();
        }
        
        if(e.getKeyCode() == Constants.controlConfirm)
        {
            select();
        }
        
        if(e.getKeyCode() == Constants.controlCancel)
        {
        	exitMenu();
        }
        
        if(e.getKeyCode() == Constants.controlQuit)
        {
        	System.exit(0);
        }
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	

}
